import { Component, OnInit } from '@angular/core';
import { AbstractControl, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { Observable } from 'rxjs/internal/Observable';
import { HttpClient } from '@angular/common/http';
import { User } from './user';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  myForm: FormGroup;
  userName: AbstractControl;
  password: AbstractControl;
  baseUrl = 'http://127.0.0.1:8080/';

  constructor(fb: FormBuilder, private httpClient: HttpClient) {
    this.myForm = fb.group(
      {
        'userName': ['lin', Validators.compose([Validators.required, Validators.maxLength(20), Validators.minLength(3)])],
        'password': ['123456', Validators.compose([Validators.required, Validators.minLength(6), Validators.maxLength(16)])],

      }
    );

    this.userName = this.myForm.controls['userName'];
    this.password = this.myForm.controls['password'];

  }

  onSubmit(v: any) {
    console.log(v);
    this.httpClient.post(this.baseUrl + "user", v).subscribe(
      (val: any) => {
        if (val.succ) {
          alert("登录成功！");
          window.location.href="https://bilibili.com"
        } else {
          alert('用户名或密码错误！');
        }
      }
    );

  }

}
